﻿using System;
using System.Collections.Generic;
using System.Linq;
using Foothill.Common;
using Foothill.Model;

namespace Foothill.Filter.Test.TestUtilities
{
    internal class TestUtility
    {
        private static IList<int> _cityIdList;

        static TestUtility()
        {
            _cityIdList = new List<int> { (int)City.Vancouver, (int)City.Richmond, (int)City.Burnaby, (int)City.Surrey, (int)City.NorthVancouver, (int)City.Coquitlam, (int)City.Delta };
        }

        internal static ForSaleAd ConstructNewSaleAd(ModelId modelId, MakeId makeId, VehicleCondition condition,
           int year, int mileAge, TransmissionType transmissionType, int price, ListingTypes listingType,
           City city, string phoneNumber, string email, DateTime timeEdited)
        {
            ForSaleAd newAd = new ForSaleAd();

            newAd.ModelId = Convert.ToInt32(modelId);
            newAd.MakeId = Convert.ToInt32(makeId);
            newAd.VehicleConditionId = Convert.ToInt32(condition);
            newAd.YearMade = year;
            newAd.MileAge = mileAge;
            newAd.MileAgeStr = mileAge.ToString();
            newAd.TransmissionTypeId = Convert.ToInt32(transmissionType);
            newAd.Price = price;
            newAd.PriceStr = price.ToString();
            newAd.ListingTypeId = Convert.ToInt32(listingType);
            newAd.CityId = Convert.ToInt32(city);
            newAd.ContactPhone = phoneNumber;
            newAd.ContactEmail = email;
            newAd.TimePosted = DateTime.Now;
            newAd.LastEditTime = timeEdited;
            newAd.StatusId = (int)AdStatusLookup.Active;

            return newAd;
        }

        internal static UploadPhoto ConstructProfilePhoto(int adId)
        {
            Guid guid = Guid.NewGuid();
            UploadPhoto result = new UploadPhoto { AdId = adId, PathOnServer = guid.ToString(), UploadDateTime = DateTime.Now };
            return result;
        }

        // construct search param for default list
        internal static ForSaleAdAdvancedSearchParameter ConstructSimpleSearchParameter(MakeId make, ModelId model, int yearFrom, int yearTo, int kiloFrom, int kiloTo,
            int priceFrom, int priceTo, VehicleCondition condition, ListingTypes listingType, int pageNum, int pageSize,
            OrderByItems orderByItem, fhSortDirection sortDirection)
        {
            ForSaleAdAdvancedSearchParameter result = new ForSaleAdAdvancedSearchParameter(_cityIdList);

            result.AddMakeAndModel((int)make, (int)model);

            // year 
            result.AddRangeBasedAdSearchCriteria(RangeBasedCriteria.YearMade, yearFrom, yearTo);

            // kilometers
            result.AddRangeBasedAdSearchCriteria(RangeBasedCriteria.Kilometer, kiloFrom, kiloTo);

            // price
            result.AddRangeBasedAdSearchCriteria(RangeBasedCriteria.Price, priceFrom, priceTo);

            // condition
            result.ConditionId = (int)condition;

            // listing type
            result.ListingTypeId = (int)listingType;

            // with photo only
            result.WithPhotosOnly = true;

            // location list
            result.AllLocationSelected = true;

            // page size and page num
            result.PageNum = pageNum;
            result.PageSize = pageSize;

            result.OrderByItem = orderByItem;
            result.SortDirection = sortDirection;

            result.FilterList = new List<AdCategorySelectItem>();

            return result;
        }

        // construct search param for default list
        internal static ForSaleAdAdvancedSearchParameter ConstructSimpleSearchParameter(MakeId make, ModelId model, int yearFrom, int yearTo, int kiloFrom, int kiloTo,
            int priceFrom, int priceTo, VehicleCondition condition, ListingTypes listingType, TransmissionType tranType, int pageNum, int pageSize,
            OrderByItems orderByItem, fhSortDirection sortDirection)
        {
            ForSaleAdAdvancedSearchParameter result = ConstructSimpleSearchParameter(make, model, yearFrom, yearTo, kiloFrom, kiloTo,
              priceFrom, priceTo, condition, listingType, pageNum, pageSize, orderByItem, sortDirection);

            result.TransmissionTypeId = (int)tranType;

            return result;
        }

        internal static bool UnorderedEqual<T>(IList<T> list1, IList<T> list2, IEqualityComparer<T> equalityComparer)
        {
            if (list1.Count != list2.Count)
            {
                return false;
            }

            if (list1.Except(list2, equalityComparer).Count() != 0)
            {
                return false;
            }

            if (list2.Except(list1, equalityComparer).Count() != 0)
            {
                return false;
            }

            return true;
        }
    }

    public enum ModelId
    {
        EL = 1,
        RSX = 2,
        Yaris = 3,
        Camry = 4,
        Taurus = 5,
        Mustang = 6,
        Jetta = 7,
        Rabbit = 8
    }

    public enum MakeId
    {
        Acura = 1,
        Toyota = 2,
        Ford = 3,
        VolksWagen = 4
    }

    public enum City
    {
        Vancouver = 1,
        Richmond = 2,
        Burnaby = 3,
        Surrey = 4,
        NorthVancouver = 5,
        Coquitlam = 6,
        Delta = 7
    }
}
